package net.alan.modules.service.impl;

import net.alan.common.constants.EProjectLogType;
import net.alan.modules.bean.*;
import net.alan.modules.repository.DailyItemRepository;
import net.alan.modules.repository.DailyRepository;
import net.alan.modules.repository.WeeklyItemRepository;
import net.alan.modules.repository.WeeklyRepository;
import net.alan.modules.service.IProjectService;
import net.alan.modules.service.IReportService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Author 李岩飞
 * Email eliyanfei@126.com
 * 2017/11/21
 */
@Service
@Transactional
public class ReportServiceImpl extends BaseServiceImpl implements IReportService {
    private static final Logger logger = LoggerFactory.getLogger(ReportServiceImpl.class);
    @Autowired
    private DailyRepository dailyRepository;
    @Autowired
    private DailyItemRepository dailyItemRepository;
    @Autowired
    private WeeklyRepository weeklyRepository;
    @Autowired
    private WeeklyItemRepository weeklyItemRepository;
    @Autowired
    private IProjectService projectService;

    @Override
    public JpaRepository getJpaRepository(Class<?> cla) {
        if (isSame(cla, Daily.class)) {
            return dailyRepository;
        } else if (isSame(cla, DailyItem.class)) {
            return dailyItemRepository;
        } else if (isSame(cla, Weekly.class)) {
            return weeklyRepository;
        } else if (isSame(cla, WeeklyItem.class)) {
            return weeklyItemRepository;
        }
        return null;
    }

    @Override
    public void afterSave(Object obj) {
        if (isSame(obj.getClass(), Daily.class)) {
            Daily task = (Daily) obj;
            projectService.addProjectLog(task.getProjectId(), task.getId(), EProjectLogType.common, "发表日报");
        } else if (isSame(obj.getClass(), Weekly.class)) {
            Weekly task = (Weekly) obj;
            projectService.addProjectLog(task.getProjectId(), task.getId(), EProjectLogType.common, "发表周报");
        }
    }

    @Override
    public void afterDelete(Object obj) {
        if (isSame(obj.getClass(), Daily.class)) {
            Daily daily = (Daily) obj;
            projectService.addProjectLog(daily.getProjectId(), daily.getId(), EProjectLogType.common, "删除日报");
        } else if (isSame(obj.getClass(), Weekly.class)) {
            Weekly weekly = (Weekly) obj;
            projectService.addProjectLog(weekly.getProjectId(), weekly.getId(), EProjectLogType.common, "删除周报");
        }
    }


    @Override
    public List<Daily> listDailyByProjectId(Integer projectId, Integer day) {
        return this.dailyRepository.findByProjectIdAndDayOrderByIdDesc(projectId, day);
    }

    @Override
    public List<DailyItem> listDailyItemByDailyId(Integer dailyId) {
        return this.dailyItemRepository.findByDailyId(dailyId);
    }

    @Override
    public List<Weekly> listWeeklyByProjectId(Integer projectId, Integer week) {
        return this.weeklyRepository.findByProjectIdAndWeekOrderByIdDesc(projectId, week);
    }

    @Override
    public List<WeeklyItem> listWeelyItemByDailyId(Integer weekly) {
        return this.weeklyItemRepository.findByWeeklyId(weekly);
    }

    @Override
    public List<Object[]> statListDaily(Integer projectId, int month) {
        return this.dailyRepository.statList(projectId, month);
    }

    @Override
    public List<Object[]> statListWeekly(Integer projectId, int month) {
        return this.weeklyRepository.statList(projectId, month);
    }
}
